//
//  WHYLeftDockMenu.m
//  News163
//
//  Created by 王洪运 on 15/12/31.
//  Copyright © 2015年 王洪运. All rights reserved.
//

#import "WHYLeftDockMenu.h"
#import "UIButton+WHYExtension.h"

@implementation WHYLeftDockMenu

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = WHYRGBColor(200, 200, 200);
        
        [self setupButtonWithImage:@"sidebar_nav_news" title:@"新闻"];
        [self setupButtonWithImage:@"sidebar_nav_reading" title:@"订阅"];
        [self setupButtonWithImage:@"sidebar_nav_photo" title:@"图片"];
        [self setupButtonWithImage:@"sidebar_nav_video" title:@"视频"];
        [self setupButtonWithImage:@"sidebar_nav_comment" title:@"跟帖"];
        [self setupButtonWithImage:@"sidebar_nav_radio" title:@"电台"];
        
    }
    
    return self;
}

-(UIButton *)setupButtonWithImage:(NSString *)image title:(NSString *)title {
    
    UIButton *button = [UIButton buttonWithImage:image
                                                            backImage:nil
                                                            title:title
                                                            titleColor:[UIColor whiteColor]
                                                            font:17
                                                            forState:UIControlStateNormal];
    
    button.tag = self.subviews.count;
    [self addSubview:button];
    
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    
    return button;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    NSUInteger count = self.subviews.count;
    
    CGFloat buttonW = self.width;
    CGFloat buttonH = self.height / count;
    
    for (int i = 0; i < count; i++) {
        
        UIButton *button = self.subviews[i];
        button.width = buttonW;
        button.height = buttonH;
        button.y = i * buttonH;
        
    }
}

-(void)clickButton:(UIButton *)button{
    
    [WHYDefaultNotificationCenter postNotificationName:WHYLeftDockMenuDidSelectedNotification object:nil userInfo:@{WHYLeftDockMenuSelectedIndexKey : @(button.tag)}];
    
}

@end
